////////////////////////////////////////////////////////////////////////////////
//////// ELECTED LEADERS
////////////////////////////////////////////////////////////////////////////////

/*
This do-file associates each election with the member of the executive appointed following the election (if any).
We design two methods to determine this variable:
	1.	using variables defined at the country*year level in V-Dem;
	2.	using the characteristics of regimes.
We use the first method as baseline, and manually check all elections for which the two methods disagree. We further check some unusual patterns in the data (see the paper appendix for more details).
*/

////////////////////////////////////////////////////////////////////////////////
//////// a. Using country*year-level data
////////////////////////////////////////////////////////////////////////////////

// We import the list of elections
use "$project_path/data/3_cleaned/presidential_elections.dta", clear
keep Country Year Month Type_Election
append using "$project_path/data/3_cleaned/parliamentary_elections.dta"
keep  Country Year Month Type_Election
tempfile election_list
save `election_list'

// We import the V-Dem country-year variables
use "$project_path/data/1_input/elections/V-Dem/vdem_cy_extract.dta", clear
/*
// Harmonizing country names
ren country_name Country
ren year Year
replace Country="Bosnia-Herz"  if Country=="Bosnia and Herzegovina"
replace Country="Myanmar"  if Country=="Burma/Myanmar"
replace Country="C. Verde Is."  if Country=="Cape Verde"
replace Country="Cent. Af. Rep."  if Country=="Central African Republic"
replace Country="PRC"  if Country=="China"
replace Country="Comoro Is."  if Country=="Comoros"
replace Country="Czech Rep."  if Country=="Czech Republic"
replace Country="Congo (DRC)"  if Country=="Democratic Republic of the Congo"
replace Country="Dom. Rep."  if Country=="Dominican Republic"
replace Country="Eq. Guinea"  if Country=="Equatorial Guinea"
replace Country="Swaziland"  if Country=="Eswatini"
replace Country="GDR"  if Country=="German Democratic Republic"
replace Country="FRG/Germany"  if Country=="Germany"
replace Country="PRK"  if Country=="North Korea"
replace Country="Macedonia"  if Country=="North Macedonia"
replace Country="P. N. Guinea"  if Country=="Papua New Guinea"
replace Country="Congo"  if Country=="Republic of the Congo"
replace Country="Solomon Is."  if Country=="Solomon Islands"
replace Country="S. Africa"  if Country=="South Africa"
replace Country="Korea, Republic of"  if Country=="South Korea"
replace Country="Gambia"  if Country=="The Gambia"
replace Country="Trinidad-Tobago"  if Country=="Trinidad and Tobago"
replace Country="UAE"  if Country=="United Arab Emirates"
replace Country="UK"  if Country=="United Kingdom"
replace Country="USA"  if Country=="United States of America"
drop if Country=="Baden"
drop if Country=="Bavaria"
drop if Country=="Brunswick"
drop if Country=="Country"
drop if Country=="Hamburg"
drop if Country=="Hanover"
drop if Country=="Hesse-Darmstadt"
drop if Country=="Hesse-Kassel"
drop if Country=="Hong Kong"
drop if Country=="Mecklenburg Schwerin"
drop if Country=="Modena"
drop if Country=="Nassau"
drop if Country=="Oldenburg"
drop if Country=="Papal States"
drop if Country=="Parma"
drop if Country=="Piedmont-Sardinia"
drop if Country=="Republic of Vietnam"
drop if Country=="Saxe-Weimar-Eisenach"
drop if Country=="Saxony"
drop if Country=="Tuscany"
drop if Country=="Two Sicilies"
drop if Country=="Würtemberg"
drop if Country=="Zanzibar"
drop if Country=="Palestine/Gaza"
drop if Country=="Palestine/British Mandate" & Year>1947
drop if Country=="Palestine/West Bank" & Year<=1948
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/British Mandate"
replace Country="Occupied Palestinian Territories"  if Country=="Palestine/West Bank"


// HOS forms of power
gen hos_disleg = v2exdfdshs_ord/3 if v2exdfdshs_ord!=.
gen hos_appointcab = v2exdfcbhs_ord/4 if v2exdfcbhs_ord!=.
gen hos_discab = v2exdfdmhs_ord/3 if v2exdfdmhs_ord!=.
gen hos_veto = v2exdfvths_ord/4 if v2exdfvths_ord!=.
gen hos_propose = v2exdfpphs_ord
recode hos_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hos_propose = hos_propose/2 if hos_propose!=.

// HOG forms of power
gen hog_disleg = v2exdjdshg_ord/3 if v2exdjdshg_ord!=.
gen hog_appointcab = v2exdjcbhg_ord/2 if v2exdjcbhg_ord!=.
gen hog_discab = v2exdfdshg_ord/3 if v2exdfdshg_ord!=.
gen hog_veto = v2exdfvthg_ord/4 if v2exdfvthg_ord!=.
gen hog_propose = v2exdfpphg_ord
recode hog_propose (0=2) (2=0) //We reorder so that 0 corresponds to less power
replace hog_propose = hog_propose/2 if hog_propose!=.

// We define a power index for the HOS and HOG as the mean of the 5 power variables
egen hos_power_linear = rmean(hos_disleg hos_appointcab hos_discab hos_veto hos_propose)
egen hog_power_linear = rmean(hog_disleg hog_appointcab hog_discab hog_veto hog_propose)

// Non-democratically elected leaders
gen hos_nondemoc_year = 1 if v2expathhs==0 | v2expathhs==1 | v2expathhs==2 | v2expathhs==3 | v2expathhs==4 | v2expathhs==5
gen hog_nondemoc_year = 1 if v2expathhg==0 | v2expathhg==1 | v2expathhg==2 | v2expathhg==3 | v2expathhg==4 | v2expathhg==5

// Removing variable prefixes
ren v2ex_hosw hosw
ren v2x_polyarchy polyarchy
ren v2exhoshog hoshog
ren v2ex_elechog elechog
ren v2ex_elechos elechos
ren v2ex_legconhog legconhog
ren v2ex_legconhos legconhos 
ren v2ex_hosconhog hosconhog
ren v2expathhg path_hog
ren v2expathhs path_hos
drop v2*

// Finding how the HOS is appointed
gen hos_appointment = "Unknown"
replace hos_appointment = "Directly" if elechos==1
replace hos_appointment = "Parliament" if elechos==0 & legconhos==1
replace hos_appointment = "Non-democratic" if hos_nondemoc_year==1

// Finding how the HOG is appointed
gen hog_appointment = "Unknown"
replace hog_appointment = "Directly" if elechog==1
replace hog_appointment = "Parliament" if elechog==0 & legconhog==1
replace hog_appointment = "Appointed by HOS" if elechog==0 & hosconhog==1 & legconhog==0
replace hog_appointment = "Irrelevant: HOS is HOG" if hoshog==1
replace hog_appointment = "Non-democratic" if hog_nondemoc_year==1

// Leaders appointed following a presidential election
gen leaders_pres_elec = "HOS"
replace leaders_pres_elec = "HOG" if hos_appointment!="Directly" & hos_appointment!="Unknown" & hog_appointment=="Directly"
replace leaders_pres_elec = "HOG" if hos_appointment=="Directly" & hog_appointment=="Directly" & hosw==0

// Leaders appointed following a parliamentary election
gen leaders_parl_elec = "Unknown"
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hog_appointment!="Parliament" & hog_appointment!="Unknown"
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hosw==1
replace leaders_parl_elec = "HOG" if hog_appointment=="Parliament" & hos_appointment!="Parliament" & hos_appointment!="Unknown"
replace leaders_parl_elec = "HOG" if hog_appointment=="Parliament" & hosw==0
replace leaders_parl_elec = "HOS" if hos_appointment=="Parliament" & hog_appointment=="Appointed by HOS" & hosw==1
replace leaders_parl_elec = "HOG" if hos_appointment=="Parliament" & hog_appointment=="Appointed by HOS" & hosw==0
replace leaders_parl_elec = "No leader" if hos_appointment!="Parliament" & hog_appointment!="Parliament" & hos_appointment!="Unknown" & hog_appointment!="Unknown"

// Merging with election list 
merge 1:m Country Year using `election_list', keep(using match) nogen

gen elected_leader_cy = ""
replace elected_leader_cy = leaders_pres_elec if Type_Election=="Presidential"
replace elected_leader_cy = leaders_parl_elec if Type_Election=="Parliamentary"
replace elected_leader_cy = "Unknown" if elected_leader_cy==""

// Check that the main variable is always nonempty
assert elected_leader_cy!=""

keep Country Year Month Type_Election elected_leader_cy
sort Type_Election Country Year Month

tempfile elected_leaders_cy
save `elected_leaders_cy'

////////////////////////////////////////////////////////////////////////////////
//////// b. Using regime-level data
////////////////////////////////////////////////////////////////////////////////

// Elections-regime match
use "$project_path/data/2_intermediary/regimes/elections_regime_match", clear
merge m:1 Country regime using "$project_path/data/3_cleaned/regimes.dta", assert(match) nogen

// Elected leader
gen elected_leader_regime = ""
replace elected_leader_regime = leaders_pres_elec if Type_Election=="Presidential"
replace elected_leader_regime = leaders_parl_elec if Type_Election=="Parliamentary"
assert elected_leader_regime!=""

keep Country Year Month Type_Election elected_leader_regime
sort Type_Election Country Year Month

tempfile elected_leaders_regime
save `elected_leaders_regime'

////////////////////////////////////////////////////////////////////////////////
//////// c. Combining methods
////////////////////////////////////////////////////////////////////////////////

use `elected_leaders_cy', clear
merge 1:1 Country Year Month Type_Election using `elected_leaders_regime', assert(match) nogen

// Detecting clashes
gen elected_leader = elected_leader_cy
replace elected_leader = "Clash" if elected_leader_cy!=elected_leader_regime & elected_leader_cy!="Unknown" & elected_leader_regime!="Unknown"
lab var elected_leader "Leader appointed following the election"

tempfile elected_leaders
save `elected_leaders'

// Importing manual changes
import excel "$project_path/data/1_input/other/leaders/elected_leaders_manual.xlsx", firstrow clear
keep Country Year Month Type_Election elected_leader_manual
merge 1:1 Country Year Month Type_Election using `elected_leaders', keep(using match) nogen
replace elected_leader = elected_leader_manual if elected_leader_manual!=""

// Check that all cases have been addressed
assert elected_leader!="Clash" & elected_leader!="Unknown"

keep Country Year Month Type_Election elected_leader

save "$project_path/data/2_intermediary/leaders/elected_leaders.dta", replace
